var fso = new ActiveXObject("Scripting.FileSystemObject");
var fo = fso.GetFolder(".");
var datas = fso.CreateTextFile("shell.jsfl",true);


var totalFolderName = fo.name;

var files = new Array();
var paths = new Array();
var classNames = new Array();

function scan(f)
{

	var fc = new Enumerator(f.SubFolders);
	while(!fc.atEnd())
	{
		scan(fc.item());
		fc.moveNext();	
	}
	
	var fs = new Enumerator(f.files);
	
	
	
	while(!fs.atEnd())
	{

		
		if ( (fs.item().name.indexOf(".png")>= 0)  && fs.item().name.indexOf("svn") < 0 )
		{
			var path = "";
			var folderName = String(fs.item().parentFolder);
			var itemfolders = folderName.split("\\");
			folderName = "";
			
			for (var i=itemfolders.length - 1; i >= 0; i--)
			{
				if (itemfolders[i] != totalFolderName)
				{
					folderName = itemfolders[i] + "_" + folderName;
					path = itemfolders[i] + "/" + path;
					
				}
				else
				{
					break;	
				}
			}
			
			folderName += fs.item().name.split(".")[0];
			path += fs.item().name;
			
			paths.push(path);
			classNames.push(folderName);
			
			
			//datas.WriteLine(fs.item().name + ":" + folderName + ": file:///" + path);	
			
		}
		fs.moveNext();
	}
}

function createHeader()
{
	datas.WriteLine('fl.createDocument();');
}

function createContent()
{
	for (var i=0; i<paths.length; i++)
	{
		datas.WriteLine('fl.runScript("file:///importFile.jsfl","importFile","file:///' + paths[i] +'","' + classNames[i] + '");');	
	}	
}

function createEnd()
{
	
	datas.WriteLine('fl.getDocumentDOM().docClass = "Resource";');
	datas.WriteLine('fl.saveDocument(fl.getDocumentDOM(),"file:///Resource.fla");');
	datas.WriteLine('fl.getDocumentDOM().exportSWF("file:///Resource.swf");');
	datas.WriteLine('fl.getDocumentDOM().close(false)');
}

function createDocumentClass()
{
	var doc = fso.CreateTextFile("Resource.as",true);
	doc.WriteLine('package');
	doc.WriteLine('{');
	doc.WriteLine('import flash.display.BitmapData;');
	doc.WriteLine('import flash.display.Sprite;');
	doc.WriteLine('import flash.utils.Dictionary;');
	doc.WriteLine('public class Resource extends Sprite');
	doc.WriteLine('{');
	doc.WriteLine('private var dic:Dictionary;');
	doc.WriteLine('public function Resource()');
	doc.WriteLine('{');
	doc.WriteLine('dic = new Dictionary();');
	
	for (var i=0; i<classNames.length; i++)
	{
		doc.WriteLine('dic["' + classNames[i] +'"] = new ' + classNames[i] + '(1,1);');
	}
	
	doc.WriteLine('}');
	doc.WriteLine('public function getResource(imgKey:String):BitmapData');
	doc.WriteLine('{');
	doc.WriteLine('return dic[imgKey]');
	doc.WriteLine('}');
	doc.WriteLine('public function getCopyResource(imgKey:String):BitmapData');
	doc.WriteLine('{');
	doc.WriteLine('if (dic[imgKey])return dic[imgKey].clone();return null;');
	doc.WriteLine('}');
	doc.WriteLine('}');
	doc.WriteLine('}');
}



scan(fo);

createDocumentClass();
createHeader();
createContent();
createEnd();


datas.close();